import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
import store from "../store";


const Home = () => import("views/home/Home");
const Search = () => import("views/search/Search");
const Login = () => import("views/login/Login");
const Register = () => import("views/register/Register");
const Detail = () => import("views/detail/Detail");
const AddCartSuccess = () => import("views/addcartsuccess/AddCartSuccess");
const ShopCart = () => import("views/shopcart/ShopCart");
const Trade = () => import("views/trade/Trade");
const Pay = () => import("views/pay/Pay");
const PaySuccess = () => import("views/paysuccess/PaySuccess");
const Center = () => import("views/center/Center");
const MyOrder = () => import("views/center/MyOrder");
const GroupOrder = () => import("views/center/GroupOrder");


//先吧VueRouter原型对象的push,先保存一份
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;

//重写 push | replace
//第一个参数: 告诉原来的push()方法 你往哪里跳(传递那些参数)
//第二个参数: 成功的回调
//第三个参数: 失败的回调
VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject)
  } else {
    originPush.call(this, location, () => {
    }, () => {
    })
  }
}

VueRouter.prototype.replace = function (location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject)
  } else {
    originReplace.call(this, location, () => {
    }, () => {
    })
  }
}


const routes = [
  //redirect重定向
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/center',
    component: Center,
    meta: {show: true},
    children: [
      {
        path: '/center',
        redirect: '/center/myorder'
      },
      {
        path: 'myorder',
        component: MyOrder
      },
      {
        path: 'grouporder',
        component: GroupOrder
      }
    ]
  },
  {
    path: '/paysuccess',
    component: PaySuccess,
    meta: {show: true}
  },
  {
    path: '/pay',
    component: Pay,
    meta: {show: true},
    beforeEnter: (to, from, next) => {
      // console.log(from.path)
      if (from.path === '/trade') {
        next()
      } else {
        next(false)
      }
    }
  },
  {
    path: '/trade',
    component: Trade,
    meta: {show: true},
    beforeEnter: (to, from, next) => {
      //去交易界面， 必须是从购物车而来
      // console.log(from.path)
      if (from.path === '/shopcart') {
        next()
      } else {
        next(false)
      }
    }
  },
  {
    path: '/shopcart',
    component: ShopCart,
    meta: {show: true}
  },
  {
    name: 'addcartsuccess',
    path: '/addcartsuccess',
    component: AddCartSuccess,
    meta: {show: true}
  },
  {
    path: '/detail/:skuid',
    component: Detail,
    meta: {show: true}
  },
  {
    path: '/home',
    component: Home,
    meta: {
      show: true
    }
  },
  {
    path: '/search/:keyword?',
    name: 'search',
    component: Search,
    meta: {
      show: true
    },
    //4: 路由组件能不能传递props数据?
    //布尔值写法 : params
    // props: true
    //对象写法 额外的给路由组件传递一些props
    // props: {a:1, b:2}
    //函数写法: 通过props传递给路由组件
    /*props:($route) => {
      return {
        keyword: $route.params.keyword,
        k: $route.query.k
      }
    }*/

  },
  {
    path: '/login',
    component: Login,
    meta: {
      show: false
    }
  },
  {
    path: '/register',
    component: Register,
    meta: {
      show: false
    }
  },

]

const router = new VueRouter({
  mode: 'history',
  routes,
  scrollBehavior() {
    //滚动条在最上方
    return {y: 0}
  }
})

//全局前置守卫
router.beforeEach(async (to, from, next) => {
  //token
  let token = store.state.user.token;
  //用户信息
  let name = store.state.user.userInfo.name;
  //登录了
  if (token) {
    // 用户已经登录了 不能进login register界面
    if (to.path === '/login' || to.path === '/register') {
      next('/home')
    } else {
      //登录了 跳转的不是(login register) --->【home, search, detail, shopCart】
      //如果有用户名
      if (name) {
        // console.log(name)
        next()
      } else {
        //如果没有用户信息, 派发action
        try {
          //获取用户信息成功
          await store.dispatch('getUserInfo')
          // console.log(result)
          next()
        } catch (e) {
          // token失效了 获取不到用户信息 重新登录 ---> 清除token
          await store.dispatch('userLogout');
          next('/login')
        }

      }
    }
  } else {
    //未登录 : 不能去交易相关、 不能去支付相关[pay, paysuccess]、不能去个人中心
    //未登录去上面的这些路由 ---->登录
    let toPath = to.path
    if (
      toPath.indexOf('/trade') !== -1 ||
      toPath.indexOf('/pay') !== -1 ||
      toPath.indexOf('/center') !== -1
    ) {
      //把未登录的时候想去而没有去成的信息， 存储于地址栏中， 【路由】
      next('/login?redirect=' + toPath)
    } else {
      //  去的不是上面这些路由 --->放行
      next()
    }


    next()
  }

})


export default router